// pages/online-refill/online-refill.js
Page({
	data: {
		patientName: '',
		department: '',
		subDepartment: '',
		campus: '',
		prescriptionId: '',
		prescriptionDate: '',
		doctorName: '',
		diagnosis: '',
		deliveryType: 'self', // self: 自取, home: 配送
		medicineList: [],
		totalMedicineCost: 0,
		totalCost: 0
	},

	onLoad(options) {
		console.log('在线配药页面加载')
		console.log('页面参数:', options)
		
		if (options.patient) {
			this.setData({
				patientName: options.patient
			})
		}
		
		if (options.department) {
			this.setData({
				department: options.department
			})
		}
		
		if (options.subDepartment) {
			this.setData({
				subDepartment: options.subDepartment
			})
		}
		
		if (options.campus) {
			this.setData({
				campus: options.campus
			})
		}
		
		if (options.prescriptionId) {
			this.setData({
				prescriptionId: options.prescriptionId
			})
			// 根据处方ID加载对应的药品信息
			this.loadMedicineByPrescription(options.prescriptionId)
		}
		
		// 计算总费用
		this.calculateTotalCost()
	},

	// 根据处方ID加载药品信息
	loadMedicineByPrescription(prescriptionId) {
		const medicineData = this.getMedicineByPrescription(prescriptionId)
		
		this.setData({
			medicineList: medicineData.medicineList,
			prescriptionDate: medicineData.prescriptionDate,
			doctorName: medicineData.doctorName,
			diagnosis: medicineData.diagnosis
		})
		
		// 重新计算总费用
		this.calculateTotalCost()
	},

	// 获取处方对应的药品信息
	getMedicineByPrescription(prescriptionId) {
		const medicineData = {
			'001': {
				prescriptionDate: '2025-08-01',
				doctorName: '张医生',
				diagnosis: '上呼吸道感染',
				medicineList: [
					{
						name: '阿莫西林胶囊',
						specification: '0.25g*24粒/盒',
						usage: '口服',
						dosage: '一次1粒，一日3次',
						days: 7,
						price: 15.80
					},
					{
						name: '布洛芬缓释胶囊',
						specification: '0.3g*12粒/盒',
						usage: '口服',
						dosage: '一次1粒，一日2次',
						days: 5,
						price: 12.50
					},
					{
						name: '复方甘草片',
						specification: '24片/盒',
						usage: '含服',
						dosage: '一次2片，一日3次',
						days: 7,
						price: 8.90
					}
				]
			},
			'002': {
				prescriptionDate: '2025-07-15',
				doctorName: '张医生',
				diagnosis: '支气管炎',
				medicineList: [
					{
						name: '头孢克肟分散片',
						specification: '0.1g*12片/盒',
						usage: '口服',
						dosage: '一次1片，一日2次',
						days: 10,
						price: 28.60
					},
					{
						name: '氨溴特罗片',
						specification: '20片/盒',
						usage: '口服',
						dosage: '一次1片，一日2次',
						days: 10,
						price: 45.80
					}
				]
			},
			'003': {
				prescriptionDate: '2025-08-02',
				doctorName: '李医生',
				diagnosis: '慢性胃炎',
				medicineList: [
					{
						name: '奥美拉唑肠溶胶囊',
						specification: '20mg*14粒/盒',
						usage: '口服',
						dosage: '一次1粒，一日1次',
						days: 14,
						price: 35.20
					},
					{
						name: '枸橼酸铋钾胶囊',
						specification: '0.3g*24粒/盒',
						usage: '口服',
						dosage: '一次1粒，一日3次',
						days: 14,
						price: 18.90
					}
				]
			},
			'005': {
				prescriptionDate: '2025-08-05',
				doctorName: '王医生',
				diagnosis: '高血压',
				medicineList: [
					{
						name: '硝苯地平缓释片',
						specification: '30mg*7片/盒',
						usage: '口服',
						dosage: '一次1片，一日1次',
						days: 28,
						price: 42.50
					},
					{
						name: '氢氯噻嗪片',
						specification: '25mg*100片/瓶',
						usage: '口服',
						dosage: '一次1片，一日1次',
						days: 30,
						price: 15.80
					}
				]
			},
      '014': {
        prescriptionDate: '2025-08-06',
        doctorName: '张医生',
        diagnosis: '上呼吸道感染',
        medicineList: [
          { name: '阿奇霉素片', specification: '250mg*6片/盒', usage: '口服', dosage: '一次1片，一日1次', days: 3, price: 22.60 },
          { name: '氨溴索口服溶液', specification: '100ml/瓶', usage: '口服', dosage: '10ml/次，一日3次', days: 5, price: 16.50 }
        ]
      },
      '015': {
        prescriptionDate: '2025-08-08',
        doctorName: '刘医生',
        diagnosis: '急性咽炎',
        medicineList: [
          { name: '萘敏维滴剂', specification: '10ml/瓶', usage: '含服', dosage: '5滴/次，一日3次', days: 5, price: 19.80 },
          { name: '西地碘含片', specification: '24片/盒', usage: '含服', dosage: '一次1片，一日3次', days: 5, price: 14.90 }
        ]
      },
      '016': {
        prescriptionDate: '2025-08-08',
        doctorName: '王医生',
        diagnosis: '冠心病随访',
        medicineList: [
          { name: '阿司匹林肠溶片', specification: '100mg*30片/盒', usage: '口服', dosage: '一次1片，一日1次', days: 30, price: 23.50 },
          { name: '他汀类药物', specification: '10mg*7片/盒', usage: '口服', dosage: '一次1片，一日1次', days: 28, price: 35.80 }
        ]
      },
			'009': {
				prescriptionDate: '2025-08-04',
				doctorName: '钱医生',
				diagnosis: '甲状腺结节',
				medicineList: [
					{
						name: '左甲状腺素钠片',
						specification: '50μg*30片/盒',
						usage: '口服',
						dosage: '一次1片，一日1次',
						days: 30,
						price: 25.60
					},
					{
						name: '硒酵母片',
						specification: '50μg*60片/盒',
						usage: '口服',
						dosage: '一次1片，一日2次',
						days: 30,
						price: 38.90
					}
				]
			},
			'012': {
				prescriptionDate: '2025-08-07',
				doctorName: '周医生',
				diagnosis: '湿疹',
				medicineList: [
					{
						name: '糠酸莫米松乳膏',
						specification: '10g/支',
						usage: '外用',
						dosage: '一日2次，涂抹患处',
						days: 14,
						price: 18.50
					},
					{
						name: '氯雷他定片',
						specification: '10mg*6片/盒',
						usage: '口服',
						dosage: '一次1片，一日1次',
						days: 7,
						price: 12.80
					},
					{
						name: '维生素E软胶囊',
						specification: '100mg*30粒/盒',
						usage: '口服',
						dosage: '一次1粒，一日2次',
						days: 14,
						price: 22.40
					}
				]
			},
			'017': {
				prescriptionDate: '2025-08-08',
				doctorName: '周医生',
				diagnosis: '皮炎',
				medicineList: [
					{
						name: '地奈德乳膏',
						specification: '15g/支',
						usage: '外用',
						dosage: '每日2次',
						days: 10,
						price: 21.50
					},
					{
						name: '非索非那定片',
						specification: '60mg*10片/盒',
						usage: '口服',
						dosage: '一次1片，一日2次',
						days: 7,
						price: 29.90
					}
				]
			}
		}

		return medicineData[prescriptionId] || {
			prescriptionDate: '2025-08-08',
			doctorName: '医生',
			diagnosis: '诊断',
			medicineList: [
				{
					name: '阿莫西林胶囊',
					specification: '0.25g*24粒/盒',
					usage: '口服',
					dosage: '一次1粒，一日3次',
					days: 7,
					price: 15.80
				}
			]
		}
	},

	calculateTotalCost() {
		let totalMedicineCost = 0
		this.data.medicineList.forEach(medicine => {
			totalMedicineCost += medicine.price
		})
		
		let totalCost = totalMedicineCost
		if (this.data.deliveryType === 'home') {
			totalCost += 10 // 配送费
		}
		
		this.setData({
			totalMedicineCost: totalMedicineCost.toFixed(2),
			totalCost: totalCost.toFixed(2)
		})
	},

	selectDelivery(e) {
		const deliveryType = e.currentTarget.dataset.type
		console.log('选择配送方式:', deliveryType)
		
		this.setData({
			deliveryType: deliveryType
		})
		
		// 重新计算总费用
		this.calculateTotalCost()
	},

	confirmRefill() {
		console.log('确认配药')
		console.log('患者:', this.data.patientName)
		console.log('处方号:', this.data.prescriptionId)
		console.log('配送方式:', this.data.deliveryType)
		console.log('总费用:', this.data.totalCost)
		
		wx.showModal({
			title: '确认配药',
			content: `确认要为 ${this.data.patientName} 配药吗？\n总费用：¥${this.data.totalCost}`,
			success: (res) => {
				if (res.confirm) {
					// 显示支付中
					wx.showLoading({
						title: '支付中...'
					})
					
					// 模拟支付过程
					setTimeout(() => {
						wx.hideLoading()
						
						// 跳转到配药成功页面
						wx.redirectTo({
							url: '/pages/refill-success/refill-success?patient=' + this.data.patientName + 
								'&prescriptionId=' + this.data.prescriptionId + 
								'&deliveryType=' + this.data.deliveryType + 
								'&medicineCost=' + this.data.totalMedicineCost + 
								'&totalCost=' + this.data.totalCost + 
								'&campus=' + this.data.campus
						})
					}, 2000)
				}
			}
		})
	}
})
